﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.GameState.MenuContext
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.GameState
{
  public class MenuContext : MBObjectBase
  {
    [SaveableField(102)]
    private MenuContext.MenuContextState _currentState;

    internal static void AutoGeneratedStaticCollectObjectsMenuContext(
      object o,
      List<object> collectedObjects)
    {
      ((MBObjectBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    internal static object AutoGeneratedGetMemberValue_currentState(object o)
    {
      return (object) ((MenuContext) o)._currentState;
    }

    public GameMenu GameMenu { get; private set; }

    public IMenuContextHandler Handler { get; set; }

    public string CurrentBackgroundMeshName { get; private set; }

    public string CurrentAmbientSoundID { get; private set; }

    public string CurrentPanelSoundID { get; private set; }

    public void Refresh()
    {
      this._currentState = MenuContext.MenuContextState.RequiresCreation;
      this.HandleStates();
    }

    public void SwitchToMenu(string menuId)
    {
      Campaign.Current.GameMenuManager.SetNextMenu(menuId);
      this.HandleStates();
    }

    private void HandleStates()
    {
      if (this._currentState == MenuContext.MenuContextState.Finalized)
        return;
      if (!string.IsNullOrEmpty(Campaign.Current.GameMenuManager.NextGameMenuId))
        this._currentState = MenuContext.MenuContextState.RequiresCreation;
      while (this._currentState == MenuContext.MenuContextState.RequiresCreation)
      {
        if (this.GameMenu != null)
          this.GameMenu.PreInit(this);
        if (Campaign.Current.GameMenuManager.NextGameMenuId != null)
        {
          this.GameMenu = Campaign.Current.GameMenuManager.NextMenu;
          Campaign.Current.GameMenuManager.SetNextMenu((string) null);
          MapState mapState = Game.Current.GameStateManager.LastOrDefault<MapState>();
          if (mapState != null)
            mapState.GameMenuId = this.GameMenu.StringId;
        }
        if (this.GameMenu.AutoSelectFirst)
          Campaign.Current.GameMenuManager.RunConsequenceOfVirtualMenuOption(this, 0);
        this.GameMenu.RunOnInit(Game.Current, this);
        if (string.IsNullOrEmpty(Campaign.Current.GameMenuManager.NextGameMenuId) && this._currentState != MenuContext.MenuContextState.Finalized)
          this._currentState = MenuContext.MenuContextState.RequiresInitialization;
      }
      if (this._currentState != MenuContext.MenuContextState.RequiresInitialization)
        return;
      this._currentState = MenuContext.MenuContextState.None;
      if (this.Handler != null)
        this.Handler.OnMenuCreate();
      Campaign.Current.GameMenuCallbackManager.InitializeState(this.GameMenu.StringId, this);
      if (this.Handler == null)
        return;
      this.Handler.OnMenuActivate();
    }

    public void Destroy()
    {
      this._currentState = MenuContext.MenuContextState.Finalized;
      if (!(Game.Current.GameStateManager.ActiveState is MapState activeState))
        return;
      activeState.GameMenuId = (string) null;
    }

    public void OnTick(float dt) => Campaign.Current.GameMenuManager.OnFrameTick(this, dt);

    public void OnHourlyTick() => this.Handler?.OnHourlyTick();

    public object GetCurrentRepeatableObject()
    {
      return Campaign.Current.GameMenuManager.ObjectGetCurrentRepeatableObject(this);
    }

    public object GetSelectedObject()
    {
      return Campaign.Current.GameMenuManager.GetSelectedRepeatableObject(this);
    }

    public object GetSelectedRepeatableObject()
    {
      return Campaign.Current.GameMenuManager.GetSelectedRepeatableObject(this);
    }

    public void SetRepeatObjectList(IEnumerable<object> list)
    {
      Campaign.Current.GameMenuManager.SetRepeatObjectList(this, list);
    }

    public void OnConsequence(GameMenuOption gameMenuOption)
    {
      if (Campaign.Current == null)
        return;
      Campaign.Current.GameMenuCallbackManager.OnConsequence(this.GameMenu.StringId, gameMenuOption, this);
    }

    public void InvokeConsequence(int index)
    {
      if (Campaign.Current.CurrentMenuContext != this)
        return;
      Campaign.Current.GameMenuManager.RunConsequenceOfVirtualMenuOption(this, index);
    }

    public void CloseEvent()
    {
      if (Settlement.CurrentSettlement == null)
        return;
      Campaign.Current.MapEventManager.FinalizePlayerMapEvent();
      Campaign.Current.autoEnterTown = Settlement.CurrentSettlement.Party;
      Game.Current.GameStateManager.PopState();
    }

    public void SetBackgroundMeshName(string name)
    {
      this.CurrentBackgroundMeshName = name;
      this.Handler?.OnBackgroundMeshNameSet(name);
    }

    public void SetPanelSound(string panelSoundID)
    {
      this.CurrentPanelSoundID = panelSoundID;
      this.Handler?.OnPanelSoundIDSet(panelSoundID);
    }

    public void SetAmbientSound(string ambientSoundID)
    {
      this.CurrentAmbientSoundID = ambientSoundID;
      this.Handler?.OnAmbientSoundIDSet(ambientSoundID);
    }

    public void OpenTownManagement() => this.Handler?.OnOpenTownManagement();

    public void OpenRecruitVolunteers() => this.Handler?.OnOpenRecruitVolunteers();

    public void OpenTournamentLeaderboards() => this.Handler?.OnOpenTournamentLeaderboard();

    public void OpenTroopSelection(
      TroopRoster fullRoster,
      TroopRoster initialSelections,
      Func<CharacterObject, bool> canChangeStatusOfTroop,
      Action<TroopRoster> onDone,
      int maxSelectableTroopCount,
      int minSelectableTroopCount = 1)
    {
      this.Handler?.OnOpenTroopSelection(fullRoster, initialSelections, canChangeStatusOfTroop, onDone, maxSelectableTroopCount, minSelectableTroopCount);
    }

    internal enum MenuContextState
    {
      None,
      RequiresCreation,
      RequiresInitialization,
      Finalized,
    }
  }
}
